package cn.daniellee.plugin.ib.listener;

import cn.daniellee.plugin.ib.InfiniteBlock;
import cn.daniellee.plugin.ib.core.BlockCore;
import cn.daniellee.plugin.ib.model.Edit;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.Arrays;
import java.util.List;

public class PlayerListener implements Listener {

	@EventHandler
	public void onPlayerChat(AsyncPlayerChatEvent e) {
		Player player = e.getPlayer();
		Edit edit = BlockCore.editOperation.get(player.getName());
		if (edit != null) {
			if ("Name".equals(edit.getOperation())) {
				e.setCancelled(true);
				BlockCore.editOperation.remove(player.getName());
				if ("cancel".equalsIgnoreCase(e.getMessage())) {
					player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.edit-canceled", "&eThe edit operation has been canceled.")).replace("&", "§"));
				} else {
					int maxLength = InfiniteBlock.getInstance().getConfig().getInt("setting.name-max-length", 20);
					String name = e.getMessage();
					if (name.length() <= maxLength) {
						InfiniteBlock.getInstance().getStorage().updateBuilding(edit.getId(), "name", name);
						InfiniteBlock.getInstance().getStorage().updateBuilding(edit.getId(), "reviewed", "false");
						player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.setname-success", "&eSuccessfully edit the building name, management will review it for you as soon as possible.")).replace("&", "§"));
						BlockCore.sendRefreshMessage(player);
					} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.invalid-name", "&eName length must be less than &b{length} &echaracters.").replace("{length}", Integer.toString(maxLength))).replace("&", "§"));
				}
			} else if ("Intro".equals(edit.getOperation())) {
				e.setCancelled(true);
				if (e.getMessage().startsWith("add ") || e.getMessage().startsWith("ADD ")) {
					int maxLine = InfiniteBlock.getInstance().getConfig().getInt("setting.introduction-max-line", 10);
					if (edit.getIntroduction().size() < maxLine) {
						edit.getIntroduction().add(e.getMessage().substring(4));
						showIntroduction(e.getPlayer(), edit.getIntroduction());
					} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.reach-max-line", "&eMaximum number of rows has been reached and cannot be added.")).replace("&", "§"));
				} else if ((e.getMessage().startsWith("set ") || e.getMessage().startsWith("SET "))) {
					String[] split = e.getMessage().split(" ");
					if (split.length > 2 && split[1].matches("^\\d+$")) {
						int line = Integer.parseInt(split[1]);
						if (line > 0 && edit.getIntroduction().size() >= line) {
							edit.getIntroduction().set(line - 1, StringUtils.join(Arrays.copyOfRange(split, 2, split.length), " "));
							showIntroduction(e.getPlayer(), edit.getIntroduction());
						} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.incorrect-line", "&eIncorrect line number, please check and resend.")).replace("&", "§"));
					} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.incorrect-format", "&eIncorrect format, please check and resend.")).replace("&", "§"));
				} else if (e.getMessage().startsWith("del ") || e.getMessage().startsWith("DEL ")) {
					String[] split = e.getMessage().split(" ");
					if (split.length > 1 && split[1].matches("^\\d+$")) {
						int line = Integer.parseInt(split[1]);
						if (line > 0 && edit.getIntroduction().size() >= line) {
							edit.getIntroduction().remove(line - 1);
							showIntroduction(e.getPlayer(), edit.getIntroduction());
						} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.incorrect-line", "&eIncorrect line number, please check and resend.")).replace("&", "§"));
					} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.incorrect-format", "&eIncorrect format, please check and resend.")).replace("&", "§"));
				} else if ("ok".equalsIgnoreCase(e.getMessage())) {
					BlockCore.editOperation.remove(player.getName());
					InfiniteBlock.getInstance().getStorage().updateBuilding(edit.getId(), "introduction", new Gson().toJson(edit.getIntroduction()));
					InfiniteBlock.getInstance().getStorage().updateBuilding(edit.getId(), "reviewed", "false");
					player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.setintro-success", "&eSuccessfully edit the building introduction, management will review it for you as soon as possible.")).replace("&", "§"));
					BlockCore.sendRefreshMessage(player);
				} else if ("cancel".equalsIgnoreCase(e.getMessage())) {
					BlockCore.editOperation.remove(player.getName());
					player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.edit-canceled", "&eThe edit operation has been canceled.")).replace("&", "§"));
				} else player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.incorrect-format", "&eIncorrect format, please check and resend.")).replace("&", "§"));
			}
		}
	}

	private void showIntroduction(Player player, List<String> introduction) {
		player.sendMessage((InfiniteBlock.getInstance().getPrefix() + InfiniteBlock.getInstance().getConfig().getString("message.current-intro", "&eEdited successfully, the current introduction is:")).replace("&", "§"));
		for (int i = 0; i < introduction.size(); i++) player.sendMessage(("&e" + (i + 1) + ". &r" + introduction.get(i)).replace("&", "§"));
		player.sendMessage(InfiniteBlock.getInstance().getConfig().getString("message.intro-tips", "&e(&badd [content] &eto add a line, &bset [line] [content] &eto modify a line, &bdel [line] &eto delete a line, &bok &eto complete, &bcancel &eto cancel)").replace("&", "§"));
	}

	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent e) {
		Player player = e.getPlayer();
		BlockCore.editOperation.remove(player.getName());
	}

}
